#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
struct Person {
    char name[31];
    int year;
    int month;
    int day;
} persons[1000];

bool cmp(Person a, Person b) {
    if (a.year != b.year) {
        return a.year < b.year;
    }
    if (a.month != b.month) {
        return a.month < b.month;
    }
    return a.day < b.day;
    //return strcmp(a.name, b.name) < 0;
}

int n;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> persons[i].name >> persons[i].year >> persons[i].month
            >> persons[i].day;
    }
    sort(persons, persons + n, cmp);
    for (int i = 0; i < n; i++) {
        cout << persons[i].name << endl;
    }
    return 0;
}